package com.xuecheng.manage_cms_client.service;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.domain.cms.response.CmsCode;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.manage_cms_client.dao.CmsPageRepository;
import com.xuecheng.manage_cms_client.dao.CmsSiteRepository;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

@Service
public class PageService {
    @Autowired
    CmsPageRepository cmsPageRepository;
    @Autowired
    CmsSiteRepository cmsSiteRepository;
    @Autowired
    GridFsTemplate gridFsTemplate;
    @Autowired
    GridFSBucket gridFSBucket;

//保存html页面到服务器物理路径
public void savePageToServerPath(String pageId){

    //根据pageId查询cmsPage 一页面为基础
    CmsPage cmsPage = this.findCmsPageById(pageId);
    //得到html的文件id，从cmsPage中获取htmlFileId内容  文件生成后在页面中存在的
    String fileId = cmsPage.getHtmlFileId();
    //从gridFS中查询html文件
    InputStream inputStream = this.getFileById(fileId);
    if (inputStream==null){
        ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
    }
    //拿到文件之后要保存到物理路径得到站点信息是为物理路径服务
    //得到站点的信息
    CmsSite cmsSite = this.findCmsSiteById(cmsPage.getSiteId());
    //得到页面的物理路径
    String pagePath = cmsSite.getSitePhysicalPath()+cmsPage.getPagePhysicalPath() + cmsPage.getPageName();
    //将html文件保存到服务器物理路径上
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(new File(pagePath));
        IOUtils.copy(inputStream,fileOutputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

//根据文件id从GridFS中查询文件内容
public InputStream getFileById(String fileId){
    //文件对象
    GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
    //打开下载流
    GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
    //定义GridFsResource
    GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
    try {
        return gridFsResource.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

//根据页面id查询页面信息
public CmsPage findCmsPageById(String pageId){
    Optional<CmsPage> optional = cmsPageRepository.findById(pageId);
    if(optional.isPresent()){
        return optional.get();
    }
    return null;
}
//根据站点id查询站点信息
public CmsSite findCmsSiteById(String siteId){
    Optional<CmsSite> optional = cmsSiteRepository.findById(siteId);
    if(optional.isPresent()){
        return optional.get();
    }
    return null;
}
}
